package jt.classic.app.dms.search;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectLCManager;
import jt.classic.system.object.ObjectManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.Controller;
import org.limp.mine.DataTrimmer;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import java.io.IOException;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * DMS 全文搜索action
 *
 * @author mido
 *
 */
public class DmsSearchAction extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        Connection conn = null;

        String context = ISystem.getContextPath();

        String limit = StringTool.checkString(request.getParameter("limit"),
                "20");
        String pageNO = StringTool.checkString(request.getParameter("pageNO"));

        String nm = StringTool.checkString(request.getParameter("name"));

        String type = StringTool.checkString(request.getParameter("type"));

        String encoding = StringTool.checkString(request.getParameter(
                    "encoding"));

        IUser user = UserManager.createGuestUser();

        if (!nm.equals("")) {
            String name = null;

            try {
                if (encoding.equals("")) {
                    /*url 携带参数提交*/
                    name = new String(nm.getBytes("ISO-8859-1"), "GBK");
                } else {
                    CommonBean4HSQ cbh = new CommonBean4HSQ((HttpServletRequest) request);

                    Map params = cbh.getResource();

                    name = StringTool.checkString(params.get("name"));
                }

                request.setAttribute("name", name);

                String purviewSql = ObjectManager.getObjectPurviewSQL("FILEINFO",
                        user, 0, ">=");

                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                String innerSql = "SELECT ISYS_OBJECT_RELATION.OBJ_PKID FROM ISYS_OBJECT_RELATION,ISYS_OBJECT,(" +
                    purviewSql + ") A WHERE ISYS_OBJECT.OBJ_NAME LIKE '%" +
                    name +
                    "%' AND OBJ_KEY = 'FILEINFO' AND ISYS_OBJECT.ID = A.OBJ_ID AND ISYS_OBJECT.ID = ISYS_OBJECT_RELATION.ID";

                if (!type.equals("")) {
                    innerSql = "SELECT ISYS_OBJECT_RELATION.OBJ_PKID FROM ISYS_OBJECT_RELATION,ISYS_OBJECT,(" +
                        purviewSql +
                        ") A,DOC_BVFABRYJMQ WHERE ISYS_OBJECT.OBJ_NAME LIKE '%" +
                        name +
                        "%' AND OBJ_KEY = 'FILEINFO' AND COL_FILETYPE = '" +
                        type +
                        "' AND ISYS_OBJECT.ID = A.OBJ_ID AND ISYS_OBJECT.ID = ISYS_OBJECT_RELATION.ID AND PK_DOC_BVFABRYJMQ = ISYS_OBJECT_RELATION.OBJ_PKID";
                }

                String sql = "SELECT DOC_BVFABRYJMQ.COL_FILETYPE, COUNT(DOC_BVFABRYJMQ.PK_DOC_BVFABRYJMQ) AS TL FROM DOC_BVFABRYJMQ , (" +
                    innerSql +
                    ") A WHERE DOC_BVFABRYJMQ.PK_DOC_BVFABRYJMQ = A.OBJ_PKID GROUP BY DOC_BVFABRYJMQ.COL_FILETYPE";

                DataTrimmer trimmer = new DataTrimmer(conn);

                List typeList = trimmer.searchMultiData(sql);

                /*类别统计*/
                request.setAttribute("typeList", typeList);

                /*搜索结果*/
                String sql1 = "SELECT COUNT(ID) AS TL FROM ISYS_OBJECT, ("+purviewSql+") A WHERE ISYS_OBJECT.ID = A.OBJ_ID AND OBJ_NAME LIKE '%" +
                    name + "%' AND OBJ_KEY = 'FILEINFO'";

                Map tlMap = trimmer.searchSingleData(sql1);

                request.setAttribute("totalData", tlMap.get("TL"));

                /*类别统计*/
                request.setAttribute("typeList", typeList);

                String sql2 = "SELECT ID FROM ISYS_OBJECT, ("+purviewSql+") A WHERE ISYS_OBJECT.ID = A.OBJ_ID AND OBJ_NAME LIKE '%" +
                    name +
                    "%' AND OBJ_KEY = 'FILEINFO' ORDER BY OBJ_CREATETIME DESC";

                if (!type.equals("")) {
                    sql1 = "SELECT COUNT(ISYS_OBJECT.ID) FROM ISYS_OBJECT LEFT JOIN " +
                        "ISYS_OBJECT_RELATION ON ISYS_OBJECT.ID = ISYS_OBJECT_RELATION.ID LEFT JOIN DOC_BVFABRYJMQ ON PK_DOC_BVFABRYJMQ = OBJ_PKID WHERE ISYS_OBJECT.OBJ_NAME LIKE '%" +
                        name +
                        "%' AND ISYS_OBJECT.OBJ_KEY = 'FILEINFO' AND COL_FILETYPE = '" +
                        type + "'";

                    sql2 = "SELECT ISYS_OBJECT.ID FROM ISYS_OBJECT LEFT JOIN " +
                        "ISYS_OBJECT_RELATION ON ISYS_OBJECT.ID = ISYS_OBJECT_RELATION.ID LEFT JOIN DOC_BVFABRYJMQ ON PK_DOC_BVFABRYJMQ = OBJ_PKID WHERE ISYS_OBJECT.OBJ_NAME LIKE '%" +
                        name +
                        "%' AND ISYS_OBJECT.OBJ_KEY = 'FILEINFO' AND COL_FILETYPE = '" +
                        type + "' ORDER BY OBJ_CREATETIME DESC";
                }

                String url = context + "/dmssearch?name=" + name +
                    "&encoding=" + encoding;

                if (!type.equals("")) {
                    url += ("&type=" + type);
                }

                QueryJack plus = QueryJackFactory.getInstance()
                                                 .createQueryJack(false);

                // 设置
                plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
                plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
                plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
                plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
                plus.setParameter(QueryJackFactory.PAGINATION_COUNT, limit);
                plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

                // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
                List resultList = plus.work();

                if ((resultList != null) && !resultList.isEmpty()) {
                    ObjectLCManager olm = new ObjectLCManager(conn);

                    for (int i = 0; i < resultList.size(); i++) {
                        Map temp = (Map) resultList.get(i);
                        String oid = (String) temp.get("ID");

                        IObject obj = olm.getObjectById(oid);

                        resultList.set(i,
                            new Object[] { temp.get("PAGINATION_NUMBER"), obj });
                    }
                }

                Controller controller = plus.getController("form1");

                controller.setType("input");

                Label label = plus.getLabel();

                label.setType("href");

                Object[] result = new Object[4];
                result[0] = resultList;
                result[1] = label;
                result[2] = controller;
                result[3] = pageNO;

                request.setAttribute("result", result);
            } catch (Exception ex) {
                ISystem.catchException(request, this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        }

        request.getSession().getServletContext()
               .getRequestDispatcher("/app/dms/search.jsp")
               .forward(request, response);
    }
}
